<% add_decidim_page_title(t(".attachments_title")) %>
<div class="card" id="attachments">
  <div class="item_show__header">
    <h1 class="item_show__header-title">
      <%= t(".attachments_title") %>
      <% if allowed_to? :create, :attachment %>
        <%= link_to t("actions.attachment.new", scope: "decidim.admin"), url_for(action: :new), class: "button button__sm button__secondary new" %>
      <% end %>
    </h1>
  </div>
  <%= cell("decidim/admin/attachments_privacy_warning", attached_to.attachments) %>
  <% if attached_to.attachments.any? %>
    <div class="table-stacked">
      <table class="table-list">
        <thead>
          <tr>
            <th><%= t("models.attachment.fields.title", scope: "decidim.admin") %></th>
            <th><%= t("models.attachment.fields.collection", scope: "decidim.admin") %></th>
            <th><%= t("models.attachment.fields.content_type", scope: "decidim.admin") %></th>
            <th><%= t("models.attachment.fields.file_size", scope: "decidim.admin") %></th>
            <th><%= t("decidim.admin.actions.actions") %></th>
          </tr>
        </thead>
        <tbody>
          <% attached_to.attachments.each do |attachment| %>
            <tr data-id="<%= attachment.id %>">
              <td data-label="<%= t("models.attachment.fields.title", scope: "decidim.admin") %>">
                <%= link_to translated_attribute(attachment.title), edit_polymorphic_path([attached_to, attachment]) %><br>
              </td>
              <td data-label="<%= t("models.attachment.fields.collection", scope: "decidim.admin") %>">
                <%= translated_attribute(attachment.attachment_collection&.name) %>
              </td>
              <td data-label="<%= t("models.attachment.fields.content_type", scope: "decidim.admin") %>">
                <%= attachment.file_type %>
              </td>
              <td data-label="<%= t("models.attachment.fields.file_size", scope: "decidim.admin") %>">
                <%= attachment.file? ? number_to_human_size(attachment.file_size) : "-" %>
              </td>
              <td class="table-list__actions" data-label="<%= t("decidim.admin.actions.actions") %>">
                <button type="button" data-controller="dropdown" data-target="actions-attachment-<%= attachment.id %>" aria-label="<%= t("decidim.admin.actions.actions_label", resource: translated_attribute(attachment.title)) %>">
                  <%= icon "more-fill", class: "text-secondary" %>
                </button>

                <div class="inline-block relative">
                  <ul id="actions-attachment-<%= attachment.id %>" class="dropdown dropdown__action" aria-hidden="true">
                    <% if allowed_to? :update, :attachment, attachment: attachment %>
                      <li class="dropdown__item">
                        <%= link_to edit_polymorphic_path([attached_to, attachment]), class: "dropdown__button" do %>
                          <%= icon "pencil-line" %>
                          <%= t("actions.edit", scope: "decidim.admin") %>
                        <% end %>
                      </li>
                    <% end %>

                    <% if allowed_to? :destroy, :attachment, attachment: attachment %>
                      <hr>
                      <li class="dropdown__item">
                        <%= link_to polymorphic_path([attached_to, attachment]), method: :delete, data: { confirm: t("actions.confirm_destroy", scope: "decidim.admin") }, class: "dropdown__button" do %>
                          <%= icon "delete-bin-line" %>
                          <%= t("actions.destroy", scope: "decidim.admin") %>
                        <% end %>
                      </li>
                    <% end %>
                  </ul>
                </div>
              </td>
            </tr>
          <% end %>
        </tbody>
      </table>
    </div>
  <% end %>
</div>
